package com.lagou.edu.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */
@RefreshScope
@RestController
@RequestMapping({"/email", ""})
@Slf4j
public class EmailController {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private JavaMailSenderImpl sender;

    @Value("${spring.mail.username}")
    private String fromUsername;

    @GetMapping("{email}/{code}")
    public boolean sendMail(@PathVariable("email") String email, @PathVariable("code") String code) {

        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(fromUsername);
            simpleMailMessage.setTo(email);
            simpleMailMessage.setSubject("注册验证码");
            simpleMailMessage.setText("验证码为: " + code);
            sender.send(simpleMailMessage);
            mailSender.send(simpleMailMessage);
        } catch (Exception e) {
            log.error(e.toString());
        }

        return true;
    }
}
